package com.bidevalution.business.impl;

import java.util.Date;
import java.util.List;

import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.*;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.util.LogUtil;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.OConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bidevalution.business.service.BeSectionExpertProgressService;

@Service
public class BeSectionExpertProgressServiceImpl implements BeSectionExpertProgressService {
    @Autowired
    private BeSectionExpertProgressMapper beSectionExpertProgressMapper;
    @Autowired
    private BeSectionExpertInfoMapper beSectionExpertInfoMapper;
    @Autowired
    private BeSectionStepMapper beSectionStepMapper;
    @Autowired
    private BeSectionStepJobMapper beSectionStepJobMapper;
    @Autowired
    private BeEvalutionSummaryMapper beEvalutionSummaryMapper;
    @Autowired
    private BeEvalutionOutcomeMapper beEvalutionOutcomeMapper;
    private LogUtil logUtil = LogUtil.getLogManager(getClass());

    @Override
    public List<BeSectionExpertProgress> selectFinishList(String sectionStepId) {

        return beSectionExpertProgressMapper.selectFinishList(sectionStepId);
    }

    @Override
    public int getFinishStatus(String sectionStepId, String expertid) {
        BeSectionExpertProgress beSectionExpertProgress = selectByKey(sectionStepId, expertid);
        if (beSectionExpertProgress != null) {
            return Integer.valueOf(beSectionExpertProgress.getIsFinish());
        } else {
            return 0;
        }
    }

    @Override
    public List<BeSectionExpertProgress> selectList(String sectionStepId) {
        // TODO Auto-generated method stub
        return beSectionExpertProgressMapper.selectListByStepId(sectionStepId);
    }

    @Override
    public BeSectionExpertProgress selectByKey(String sectionStepId, String expertId) {
        BeSectionExpertProgressKey beSectionexpertProgressKey = new BeSectionExpertProgressKey();
        beSectionexpertProgressKey.setExpertId(expertId);
        beSectionexpertProgressKey.setSectionStepId(sectionStepId);
        return beSectionExpertProgressMapper.selectByPrimaryKey(beSectionexpertProgressKey);
    }

    @Override
    public void update(BeSectionExpertProgress beSectionExpertProgress) {
        beSectionExpertProgressMapper.updateByPrimaryKey(beSectionExpertProgress);
    }

    @Override
    public Boolean getFinishStatus(String sectionStepId) throws Exception {
        BeSectionStep beSectionStep = beSectionStepMapper.selectByPrimaryKey(sectionStepId);
        if (beSectionExpertProgressMapper.selectFinishList(sectionStepId).size() == beSectionExpertInfoMapper
                .getList(beSectionStep.getSectionId(), OConvertUtils.getString(0)).size()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public void addProgressList(String sectionId, String submitCode) throws Exception {
    	logUtil.info("同步环节专家进度表数据开始>>>>>>>>>>>");
        BeSectionExpertProgress beSectionExpertProgress = null;
        beSectionExpertProgressMapper.deleteBySectionId(sectionId);
        for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoMapper.getList(sectionId,"0")) {
            for (BeSectionStep beSectionStep : beSectionStepMapper.getAllStepList(sectionId)) {
                beSectionExpertProgress = new BeSectionExpertProgress();
                beSectionExpertProgress.setCreateTime(new Date());
                beSectionExpertProgress.setExpertId(beSectionExpertInfo.getSeqId());
                beSectionExpertProgress.setExpertName(beSectionExpertInfo.getName());
                beSectionExpertProgress.setIsFinish("0");
                beSectionExpertProgress.setSectionStepId(beSectionStep.getSeqId());
                beSectionExpertProgress.setSubmitCode(submitCode);
                beSectionExpertProgress.setSectionId(sectionId);
                beSectionExpertProgress.setUpdateTime(new Date());
                beSectionExpertProgressMapper.insert(beSectionExpertProgress);
            }
        }
        logUtil.info("同步环节专家进度表数据结束>>>>>>>>>>>");

    }

    /**
     * 添加后面步骤的提交码
     *
     * @param beSectionStep
     * @return
     * @see com.bidevalution.business.service.BeSectionExpertProgressService#addProgressList(com.bidevalution.dao.entity.BeSectionStep)
     */
    @Override
    public void addNextProgressList(BeSectionStep beSectionStep, String submitCode) {
        BeSectionExpertProgress beSectionExpertProgress;
        for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoMapper.getListBySectionId(beSectionStep.getSectionId())) {
            beSectionExpertProgress = selectByKey(beSectionStep.getSeqId(), beSectionExpertInfo.getSeqId());
            if (beSectionExpertProgress != null) {
                beSectionExpertProgress.setSubmitCode(submitCode);
                beSectionExpertProgressMapper.updateByPrimaryKey(beSectionExpertProgress);
            }
            for (BeSectionStep nextSectionStep : beSectionStepMapper.getNextStepList(beSectionStep.getSectionId(), beSectionStep.getOrderNum())) {
                beSectionExpertProgress = selectByKey(nextSectionStep.getSeqId(), beSectionExpertInfo.getSeqId());
                if (beSectionExpertProgress != null) {
                    beSectionExpertProgress.setSubmitCode(submitCode);
                    beSectionExpertProgressMapper.updateByPrimaryKey(beSectionExpertProgress);
                }
            }
        }

    }

    @Override
    public List<BeSectionExpertProgress> selectListBySectionId(String sectionId) {
        // TODO Auto-generated method stub
        return beSectionExpertProgressMapper.selectListBySectionId(sectionId);
    }

    @Override
    @Transactional
    public void updateStatus(String sectionId, String stepCode, String status) {
        BeSectionStep beSectionStep = beSectionStepMapper.getSectionStepByCode(sectionId, stepCode);
        if (beSectionStep != null) {
            List<BeSectionStep> beSectionSteps = beSectionStepMapper.getChildSectionStepList(sectionId, stepCode);
            beSectionSteps.add(beSectionStep);
            for (BeSectionStep _beSectionStep : beSectionSteps) {
                for (BeSectionExpertProgress beSectionExpertProgress : beSectionExpertProgressMapper.selectListByStepId(_beSectionStep.getSeqId())) {
                    beSectionExpertProgress.setIsFinish(status);
                    beSectionExpertProgress.setUpdateTime(new Date());
                    beSectionExpertProgressMapper.updateByPrimaryKey(beSectionExpertProgress);
                }
            }
        }


    }

    @Override
    public void deleteBySectionId(String sectionId) {
        beSectionExpertProgressMapper.deleteBySectionId(sectionId);
    }


    @Override
    @Transactional
    public void updateStatusById(String sectionId, String stepCode, String status, String newSubmitCode) throws Exception {
        BeSectionStep beSectionStep = beSectionStepMapper.getSectionStepByCode(sectionId, stepCode);
        if (beSectionStep != null) {
            List<BeSectionStep> beSectionSteps = beSectionStepMapper.getAllChildSectionStep(sectionId, stepCode);
            beSectionSteps.add(beSectionStep);
            for (BeSectionStep _beSectionStep : beSectionSteps) {
                //删除专家评审数据（软删除）
                BeEvalutionOutcome beEvalutionOutcome = new BeEvalutionOutcome();
                beEvalutionOutcome.setSectionId(sectionId);
                beEvalutionOutcome.setSectionStepId(_beSectionStep.getSeqId());
                beEvalutionOutcome.setIsDel("1");
                beEvalutionOutcome.setUpdateTime(DateUtil.now());
                beEvalutionOutcomeMapper.updateStatusByCode(beEvalutionOutcome);
                //更新评审进度
                BeSectionExpertProgress beSectionExpertProgress = new BeSectionExpertProgress();
                beSectionExpertProgress.setSectionStepId(_beSectionStep.getSeqId());
                beSectionExpertProgress.setIsFinish(status);
                beSectionExpertProgress.setSubmitCode(newSubmitCode);
                beSectionExpertProgress.setUpdateTime(DateUtil.now());
                beSectionExpertProgressMapper.updateStatusById(beSectionExpertProgress);
                //更新环节任务状态
                BeSectionStepJob beSectionStepJob = new BeSectionStepJob();
                beSectionStepJob.setSectionStepId(_beSectionStep.getSeqId());
                beSectionStepJob.setIsFinish(status);
                beSectionStepJob.setUpdateTime(DateUtil.now());
                beSectionStepJob.setIsAdvanceExecute(Constants.TRUE);
                beSectionStepJob.setFinishCount(0);
                beSectionStepJobMapper.updateStepJobByStepId(beSectionStepJob);
                //删除专家评审汇总结果（软删除）
                BeEvalutionSummary beEvalutionSummary = new BeEvalutionSummary();
                beEvalutionSummary.setSectionId(sectionId);
                beEvalutionSummary.setSectionStepId(_beSectionStep.getSeqId());
                beEvalutionSummary.setIsDel(Constants.ISDEL_TRUE);
                beEvalutionSummary.setUpdateTime(DateUtil.now());
                beEvalutionSummaryMapper.updateIsDelBySectionId(beEvalutionSummary);
            }
        } else {
            throw new Exception("*****未查询到标段环节信息*****");
        }
    }

    @Override
    public void deleteBySectionIdAndExpertId(String sectionId, String expertId) {
        beSectionExpertProgressMapper.deleteBySectionIdAndExpertId(sectionId, expertId);
    }

}
